const vm = new Vue({
    el: '#main',
    data: {
        realName: '',
        nation: '请选择',
        papersType: '1;身份证',
        papersNumber: '',
        isEdit: false,
        nationId: '',
    },
});


function apiready() {
    fxj.fixLayout();
    initNationPicker();
    hasGid();
}

function hasGid() {
    const gid = fxj.getSearch().get('gid');
    if (gid) {
        vm.isEdit = true;
        reqGuyInfo(gid);
    }
}

function initNationPicker() {
    const nationEl = document.getElementById('nation');
    const data = [
        {
            text: '汉族',
            value: 1,
        },
        {
            text: '壮族',
            value: 2,
        },
        {
            text: '满族',
            value: 3,
        },
        {
            text: '回族',
            value: 4,
        },
        {
            text: '苗族',
            value: 5,
        },
        {
            text: '维吾尔族',
            value: 6,
        },
        {
            text: '土家族',
            value: 7,
        },
        {
            text: '彝族',
            value: 8,
        },
        {
            text: '蒙古族',
            value: 9,
        },
        {
            text: '藏族',
            value: 10,
        },
        {
            text: '布依族',
            value: 11,
        },
        {
            text: '侗族',
            value: 12,
        },
        {
            text: '瑶族',
            value: 13,
        },
        {
            text: '朝鲜族',
            value: 14,
        },
        {
            text: '白族',
            value: 15,
        },
        {
            text: '哈尼族',
            value: 16,
        },
        {
            text: '哈萨克族',
            value: 17,
        },
        {
            text: '黎族',
            value: 18,
        },
        {
            text: '傣族',
            value: 19,
        },
        {
            text: '畲族',
            value: 20,
        },
        {
            text: '傈僳族',
            value: 21,
        },
        {
            text: '仡佬族',
            value: 22,
        },
        {
            text: '东乡族',
            value: 23,
        },
        {
            text: '高山族',
            value: 24,
        },
        {
            text: '拉祜族',
            value: 25,
        },
        {
            text: '水族',
            value: 26,
        },
        {
            text: '佤族',
            value: 27,
        },
        {
            text: '纳西族',
            value: 28,
        },
        {
            text: '羌族',
            value: 29,
        },
        {
            text: '土族',
            value: 30,
        },
        {
            text: '仫佬族',
            value: 31,
        },
        {
            text: '锡伯族',
            value: 32,
        },
        {
            text: '柯尔克孜族',
            value: 33,
        },
        {
            text: '达斡尔族',
            value: 34,
        },
        {
            text: '景颇族',
            value: 35,
        },
        {
            text: '毛南族',
            value: 36,
        },
        {
            text: '撒拉族',
            value: 37,
        },
        {
            text: '塔吉克族',
            value: 38,
        },
        {
            text: '阿昌族',
            value: 39,
        },
        {
            text: '普米族',
            value: 40,
        },
        {
            text: '鄂温克族',
            value: 41,
        },
        {
            text: '怒族',
            value: 42,
        },
        {
            text: '京族',
            value: 43,
        },
        {
            text: '基诺族',
            value: 44,
        },
        {
            text: '德昂族',
            value: 45,
        },
        {
            text: '保安族',
            value: 46,
        },
        {
            text: '俄罗斯族',
            value: 47,
        },
        {
            text: '裕固族',
            value: 48,
        },
        {
            text: '乌兹别克族',
            value: 49,
        },
        {
            text: '门巴族',
            value: 50,
        },
        {
            text: '鄂伦春族',
            value: 51,
        },
        {
            text: '独龙族',
            value: 52,
        },
        {
            text: '塔塔尔族',
            value: 53,
        },
        {
            text: '赫哲族',
            value: 54,
        },
        {
            text: '珞巴族',
            value: 55,
        },
        {
            text: '布朗族',
            value: 56,
        },
        {
            text: '其他',
            value: 57,
        },
    ];
    const picker = new Picker({
        data: [data],
        selectedIndex: [0],
        title: '选择民族',
    });

    picker.on('picker.valuechange', (selectedVal, selectedIndex) => {
        vm.nation = data[selectedIndex].text;
        vm.nationId = data[selectedIndex].value;
    });

    nationEl.addEventListener('click', () => {
        picker.show();
    });
}

async function submitGuys() {
    const reqData = {
        name: vm.realName,
        nation: vm.nationId,
        type: vm.papersType.split(';')[0],
        number: vm.papersNumber,
    };
    if (vm.isEdit) {
        reqData.gid = fxj.getSearch().get('id');
    }
    const res = await ajax({
        url: 'home/userCenter/addResident',
        data: reqData,
    });
    toast(res.msg);
    if (res.err === 0) {
        history.back();
    }
}

async function reqGuyInfo(id) {
    const nation = {
        '1': '汉族',
        '2': '壮族',
        '3': '满族',
        '4': '回族',
        '5': '苗族',
        '6': '维吾尔族',
        '7': '土家族',
        '8': '彝族',
        '9': '蒙古族',
        '10': '藏族',
        '11': '布依族',
        '12': '侗族',
        '13': '瑶族',
        '14': '朝鲜族',
        '15': '白族',
        '16': '哈尼族',
        '17': '哈萨克族',
        '18': '黎族',
        '19': '傣族',
        '20': '畲族',
        '21': '傈僳族',
        '22': '仡佬族',
        '23': '东乡族',
        '24': '高山族',
        '25': '拉祜族',
        '26': '水族',
        '27': '佤族',
        '28': '纳西族',
        '29': '羌族',
        '30': '土族',
        '31': '仫佬族',
        '32': '锡伯族',
        '33': '柯尔克孜族',
        '34': '达斡尔族',
        '35': '景颇族',
        '36': '毛南族',
        '37': '撒拉族',
        '38': '塔吉克族',
        '39': '阿昌族',
        '40': '普米族',
        '41': '鄂温克族',
        '42': '怒族',
        '43': '京族',
        '44': '基诺族',
        '45': '德昂族',
        '46': '保安族',
        '47': '俄罗斯族',
        '48': '裕固族',
        '49': '乌兹别克族',
        '50': '门巴族',
        '51': '鄂伦春族',
        '52': '独龙族',
        '53': '塔塔尔族',
        '54': '赫哲族',
        '55': '珞巴族',
        '56': '布朗族',
        '57': '其他',
    };
    const res = await ajax({
        url: 'home/userCenter/getResidentByGid',
        data: {
            gid: id,
        },
    });
    const guy = res.data[0];
    vm.realName = guy.name;
    vm.nation = nation[guy.nation_id];
    vm.papersNumber = guy.licence_number;
    vm.nationId = guy.nation_id;
}

